package com.fingard.dsp.bank.directbank.nccb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author Parker on 2019-10-11.
 * @version 1.0
 * 未测试, 客户说暂时不需要, 以后上线需测试
 */
public class NCCB01BatPayRec extends NCCB01Base implements IBatPayRec {
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        //返回ATS的对象
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        try {
            //发银行报文的XML对象
            XmlTextWriter xmlWriter = new XmlTextWriter();
            NCCB01Header header = new NCCB01Header();
            header.transCode = "paysalary.EPayProtocolQry.do";//代发工资协议号查询
            header.serialNo = p_req1988.reqHeader.transSeqID;
            header.msgType = "0";
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("AcNo", p_req1988.ownAct);
            xmlWriter.writeEndDocument();
            // 发送银行
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), header);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/RejCode");
                    String respondInfo = tmpBkReader.getFirstText("/Message/Head/RejMsg");
                    if (tmpRetCode.equals("FIN0000")) {//查询协议号成功
                        String protocolNo = "";
                        while (tmpBkReader.read("/Message/Body/List")) {
                            String status = tmpBkReader.readedXElement.getSingleText("Status");
                            String curtype = getBankConfig().getMapKeyByValue("curActMapping", tmpBkReader.readedXElement.getSingleText("Currency"));
                            if ("A".equals(status) && p_req1988.ownCur.equals(curtype)) {
                                protocolNo = tmpBkReader.readedXElement.getSingleText("ProtocolNo");
                                break;
                            }
                        }
                        if (StringHelper.hasAnyChar(protocolNo)) {
                            xmlWriter = new XmlTextWriter();
                            header = new NCCB01Header();
                            header.transCode = "paysalary.EPaySalary.do";//代发工资协议号查询
                            header.serialNo = p_req1988.reqHeader.reqSeqID;
                            header.msgType = "0";
                            //写入通用的头部节点
                            writeCommonHeader(xmlWriter, header);
                            xmlWriter.writeStartElement("Body");
                            xmlWriter.writeElementString("NowDate", Format.Date8Format.format(p_req1988.getPayDateOnly()));
                            xmlWriter.writeElementString("AcNo", p_req1988.ownAct);
                            xmlWriter.writeElementString("SalaryAcNo", p_req1988.ownAct);
                            xmlWriter.writeElementString("SalaryProtocolNo", protocolNo);
                            xmlWriter.writeElementString("SalaryTotalAmount", p_req1988.totalAmt);
                            xmlWriter.writeElementString("SalaryTotalCount", p_req1988.totalNum);
                            xmlWriter.writeElementString("ProtocolNo", protocolNo);
                            xmlWriter.writeElementString("Amount", p_req1988.totalAmt);
                            xmlWriter.writeElementString("TotalCount", p_req1988.totalNum);
                            xmlWriter.writeStartElement("List");
                            while (p_req1988.readRecord()) {
                                xmlWriter.writeStartElement("Record");
                                xmlWriter.writeElementString("PayeeAcNo", p_req1988.readedDetail.recAct);
                                xmlWriter.writeElementString("PayeeAcName", p_req1988.readedDetail.recName);
                                xmlWriter.writeElementString("PayerAmount", p_req1988.readedDetail.payAmount);
                                xmlWriter.writeEndElement();
                            }
                            xmlWriter.writeEndElement();
                            xmlWriter.writeElementString("isHaveTestCode", "1");
                            xmlWriter.writeEndDocument();
                            // 发送银行
                            tmpStrRet = sendToBank(xmlWriter.toXmlString(), header);
                            if (tmpStrRet[0].length() == 0) {
                                tmpSbMsg = new StringBuilder();
                                tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                                if (tmpBkReader != null) {
                                    //获取银行响应代码，返回值为000或002表示处理成功，否则为处理失败
                                    tmpRetCode = tmpBkReader.getFirstText("/Message/Head/RejCode");
                                    respondInfo = tmpBkReader.getFirstText("/Message/Head/RejMsg");
                                    if (tmpRetCode.equals("FIN0000")) {
                                        retAts.respCode = "0";
                                        retAts.respInfo = tmpRetCode + "+" + respondInfo;
                                        retAts.reqSeqID = p_req1988.reqBatchNO;
                                        retAts.transState = tmpRetCode + "+" + tmpBkReader.getFirstText("/Message/Body/_JnlStatus");
                                        retAts.payInfoCode = retAts.transState;
                                    } else {
                                        retAts.writeRespCode("-1");
                                        retAts.writeRespInfo(respondInfo);
                                    }
                                } else {
                                    retAts.writeRespCode("-1");
                                    retAts.writeRespInfo(tmpSbMsg.toString());
                                }
                            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                                retAts.respCode = "0";
                                retAts.respInfo = tmpStrRet[1];
                                retAts.reqSeqID = p_req1988.reqBatchNO;
                                retAts.transState = PaymentState.FAIL_Batch;
                                retAts.payInfoCode = tmpStrRet[0];
                            } else {
                                retAts.respCode = "-1";
                                retAts.respInfo = tmpStrRet[1];
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo("查询协议号查询为空! 请联系银行确认.");
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(respondInfo);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_req1988.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        // 返回ATS的对象
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
        try {
            //发银行报文的XML对象
            XmlTextWriter xmlWriter = new XmlTextWriter();
            NCCB01Header header = new NCCB01Header();
            header.transCode = "paysalary.EPaySalaryQry.do";//1.8.3	代发工资交易批次状态查询
            header.serialNo = p_req1998.reqHeader.transSeqID;
            header.msgType = "0";
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("AcNo", p_req1998.ownAct);
            xmlWriter.writeElementString("BeginDate", Format.Date8Format.format(Format.DateTimeFormat.parse(p_req1998.bgnDate)));
            xmlWriter.writeElementString("EndDate", Format.Date8Format.format(Format.DateTimeFormat.parse(p_req1998.bgnDate)));
            xmlWriter.writeElementString("pageSize", "1000");
            xmlWriter.writeElementString("currentIndex", "0");
            xmlWriter.writeElementString("recordNumber", "0");
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            // 发送银行
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), header);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/RejCode");
                    String respondInfo = tmpBkReader.getFirstText("/Message/Head/RejMsg");
                    if (tmpRetCode.equals("FIN0000")) {//查询批次号成功
                        String batchNo = "";
                        while (tmpBkReader.read("/Message/Body/List")) {
                            String amount = tmpBkReader.readedXElement.getSingleText("SalaryAmount");
                            String count = tmpBkReader.readedXElement.getSingleText("SalaryCount");
                            if (p_req1998.totalAmt.equals(amount) && p_req1998.totalNum.equals(count)) {
                                batchNo = tmpBkReader.readedXElement.getSingleText("SalaryNo");
                                break;
                            }
                        }
                        if (StringHelper.hasAnyChar(batchNo)) {
                            xmlWriter = new XmlTextWriter();
                            header = new NCCB01Header();
                            header.transCode = "paysalary.EPaySalaryDetail.do";//1.8.4	代发工资交易明细查询
                            header.serialNo = p_req1998.reqHeader.reqSeqID;
                            header.msgType = "0";
                            //写入通用的头部节点
                            writeCommonHeader(xmlWriter, header);
                            xmlWriter.writeStartElement("Body");
                            xmlWriter.writeElementString("SalaryNo", batchNo);
                            xmlWriter.writeEndElement();
                            xmlWriter.writeEndDocument();
                            // 发送银行
                            tmpStrRet = sendToBank(xmlWriter.toXmlString(), header);
                            if (tmpStrRet[0].length() == 0) {
                                tmpSbMsg = new StringBuilder();
                                tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                                if (tmpBkReader != null) {
                                    tmpRetCode = tmpBkReader.getFirstText("/Message/Head/RejCode");
                                    respondInfo = tmpBkReader.getFirstText("/Message/Head/RejMsg");
                                    if (tmpRetCode.equals("FIN0000")) {//查询成功
                                        retAts.respCode = "0";
                                        retAts.respInfo=tmpRetCode;
                                        retAts.reqSeqID = p_req1998.reqBatchNO;
                                        retAts.writeBeforeDetail();
                                        while (tmpBkReader.read("/Message/Body/list/")){
                                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                            tmpRetDetail.payAct = p_req1998.ownAct;
                                            tmpRetDetail.recAct = "";//收款人号
                                            tmpRetDetail.recName = "";//收款人名
                                            tmpRetDetail.postScript = "";//批内流水号
                                            tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("AmountSum");//金额
                                            tmpRetDetail.memo = "";//备注
                                            tmpRetDetail.result = tmpBkReader.readedXElement.getSingleText("Status");
                                            tmpRetDetail.payInfoCode = tmpRetDetail.result;
                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    } else {
                                        retAts.writeRespCode("-1");
                                        retAts.writeRespInfo(respondInfo);
                                    }
                                } else {
                                    retAts.respCode = "-1";
                                    retAts.respInfo = tmpSbMsg.toString();
                                }
                            } else {
                                retAts.respCode = "-1";
                                retAts.respInfo = tmpStrRet[1];
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo("支付批次号查询为空! 请联系银行确认.");
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(respondInfo);
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(retAts.respCode) && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_req1998.reqBatchNO;
        }
        if ("-1".equals(retAts.respCode)) {
            retAts.writeBeforeDetail();
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return null;
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return null;
    }
}
